<?php

/** 
 * AJobTool - Abstract class about Job Tool functionality (can be mutualized between job)
 *
 * @author polux
 * @abstract 
 */
abstract class AJobTool {
	// ***********************  Properties  *********************** //
	/**
	 * Job Object reference
	 * @var AJob
	 */
	protected  $_o_job;
	/**
	 * Job tool properties
	 * @var array(properties_keys => properties_values)
	 */
	protected  $_as_jobToolProperties;
	/**
	 * Job tool properties mandaotry
	 * @var array(string)
	 */
	protected  $_as_jobToolPropertiesDef;
	
	// ***********************  Constructor  *********************** //
	/**
	 * Default constructor
	 * 
	 * @param AJob $p_o_jobObj
	 */
	function __construct(AJob $p_o_jobObj) {
		$this->_o_job= $p_o_jobObj;
		$p_o_jobObj->addAJobTool($this);
		$this->_as_jobToolProperties= array();
		$this->_as_jobToolPropertiesDef= array();
		$this->defineMandatoryProperties();
	}
	
	// ***********************  Accessors  *********************** //
	/**
	 * getJobObject - Return Object reference of Job
	 * @return AJob
	 */
	function &getJobObject()
	{
		return $this->_o_job;
	}	
	/**
	 * getInternalID - Return Unique ID of this tool (build from)
	 * 
	 * (non-PHPdoc)
	 *
	 * @see AJobTool::getInternalID()
	 * @return string	Unique id of this JobTool
	 */
	final public function getInternalID() {
		return $this->getJobObject()->getInternalID().'-'.get_class($this);
	}
	/**
	 * setProperty - Set a property value
	 *
	 * @param string 	$p_s_key
	 * @param mixed 	$p_s_value
	 */
	final function setProperty($p_s_key,$p_s_value)
	{
		$this->_as_jobToolProperties[$p_s_key] = $p_s_value;
	}
	/**
	 * getPropertyValue - return internal Property value
	 *
	 * @param string 	$p_s_key
	 * @return Ambigous <NULL, array(properties_keys)>
	 * @final
	 */
	final function getPropertyValue($p_s_key)
	{
		$l_s_result = null;
		if(array_key_exists($p_s_key, $this->_as_jobToolProperties))
		{
			$l_s_result = $this->_as_jobToolProperties[$p_s_key];
		}
		return $l_s_result;
	}	
	/**
	 * checkMandatoryProperties - return true if all mandatories properties are valued in array in paramters
	 * 
	 * @param array(string => string) 	$p_as_option	Option array to validate
	 * @param boolean 					$p_b_log		Log mode
	 * @return boolean		Return true all parameter are present into $p_as_option array
	 */
	final function checkMandatoryProperties($p_as_option,$p_b_log=false)
	{
		$l_b_result = true;
		
		foreach($this->_as_jobToolPropertiesDef as $l_s_key)
		{
			if(!array_key_exists($l_s_key,$p_as_option ))
			{
				if($p_b_log)
					JobsManager::getLogManager()->addLogError($this->_o_job, "Mandatory option not founded (".$l_s_key.") for JobTool (ID:".$this->getInternalID().")");
				$l_b_result=false;
			}
		}
		return $l_b_result;
	}
	/**
	 * loadProperties - load all values in properties of current object
	 *
	 * @param array(string => string) 	$p_as_option	Option array to validate
	 * @param boolean 					$p_b_log		Log mode
	 * @throws JobGenericException 	Validation process asked and result = false
	 */
	final function loadProperties($p_as_option,$p_b_validateManda=false,$p_b_log=false)
	{
		$l_b_validated = true;
		
		// Validation process
		if($p_b_validateManda)
		{
			$l_b_validated = $this->checkMandatoryProperties($p_as_option,$p_b_log);
		}
		
		// Loading properties
		if($l_b_validated)
		{
			foreach($p_as_option as $l_s_key => $l_s_value)
			{
				$this->setProperty($l_s_key, $l_s_value);
				if($p_b_log)
					if(is_array($l_s_value))
						JobsManager::addLogJobEvent($this->_o_job, "Property (".$l_s_key.") defined to array with values '".serialize($l_s_value)."' for JobTool (ID:".$this->getInternalID().")");
					else 
						JobsManager::addLogJobEvent($this->_o_job, "Property (".$l_s_key.") defined to '".@strval($l_s_value)."' for JobTool (ID:".$this->getInternalID().")");
			}
		}
		else 
			throw new JobGenericException("Errors occured during validation of properties. Job Tool can't be used (ID:".$this->getInternalID()." !");
	}
	
	// ***********************  Abstract Methods  *********************** //
	/**
	 * RunJobTool - Run Job Tools
	 * @param 	array(ID_OPT => OPT_VALUE) 		$p_as_options	Option list value
	 * @abstract
	 */
	abstract function RunJobTool($p_as_options);
	
	/**
	 * defineMandatoryProperties - Properties mandatory
	 *
	 * @abstract
	 */
	abstract function defineMandatoryProperties();
	
	
}

?>